<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/11/8
 * Time: 16:27
 */
//管理员控制器
namespace app\admin\controller;
use app\admin\model\AuthGroup;

class Admin extends Base{


    //管理员列表
    public function index(){
//        $data = [
//            'start'=> input('param.start')?nput('param.start'):'',
//            'end'=> input('param.end')?nput('param.end'):'',
//            'username'=> input('param.username')?nput('param.username'):'',
//        ];
        $key_words = input('param.username');
        $where = [];
        if($key_words){
            $where['username']=['like',"%{$key_words}%"];
        }
        $admin_list = \app\admin\model\Admin::where($where)
            ->paginate(10,false,[
                'query'     => [
                    'username'=>$key_words,
                ]
            ]);

            $this->assign('admin_list',$admin_list);
        $this->assign('username',$key_words);
        return $this->fetch();
    }
    //添加管理员
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            if(!$data) return json(['code'=>0,'data'=>$data,'msg'=>'参数为空']);
            $AdminModel = new \app\admin\model\Admin();
            return $AdminModel->add($data);
        }else{
            //获取角色
            $role  = AuthGroup::where('status',1)->field('title,id')->select();
            $this->assign('role',$role);
            return $this->fetch();
        }
    }
    //修改状态
    public function set_status(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $status = input('param.status');
        $id = input('param.id');
        $re = \app\admin\model\Admin::where('id',$id)->update([
           'status'=>$status
        ]);
        if(!$re)  return json(['code'=>1,'data'=>'','msg'=>'修改失败']);
        return json(['code'=>200,'data'=>'','msg'=>'修改成功']);
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $data = input('param.');
            $AdminModel = new \app\admin\model\Admin();
            return $AdminModel->update_data($data);
        }else{
            $id = input('param.id');
            $auth_group = AuthGroup::all();
            $admin_data = \app\admin\model\Admin::get($id)->getData();
            $this->assign('auth_group',$auth_group);
            $this->assign('admin_data',$admin_data);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $id = input('param.');
        if(is_array($id)){
            foreach ($id as $k=>$v){
                \app\admin\model\Admin::destroy($v);
            }
        }
        return json(['code'=>200,'data'=>$id,'msg'=>'ok']);
    }
}